﻿using System;
using System.Text;
using Orid.Data.SqlServer;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class DataStore:DataBase
    {
        #region 虚函数
        public virtual int GetFileSize(Guid? DataStoreID)
        {
            return 0;
        }
        #endregion

        protected int GetFileSize(Guid? DataStoreID, string connectString, string tableName)
        {
            int fileSinze = -1 ;
            try
            {

                if (DataStoreID == null)
                {
                    return 0;
                }

                StringBuilder sqlText = new StringBuilder();
                sqlText.Append(" SELECT	 [FileSize]");
                sqlText.Append("  FROM  " + tableName + "  [DataStore] with(NOLOCK) ");
                sqlText.Append(" WHERE [DataStoreID] = @DataStoreID");

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {    
                                       new System.Data.SqlClient.SqlParameter("@DataStoreID",System.Data.SqlDbType.UniqueIdentifier), 

                                 };

                    SqlParameter[0].Value = DataStoreID;

                    object size = SqlHelper.ExecuteScalar(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                    if (size != DBNull.Value)
                    {
                        fileSinze = Convert.ToInt32(size);
                    }

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();


                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }
            return fileSinze;
        }
    }
}
